/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easemob.chatuidemo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMGroup;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import net.mihome.app.R;
import net.mihome.app.bean.URLs;

import java.util.List;

public class PickGroupAdapter extends ArrayAdapter<EMGroup> {
	private LayoutInflater inflater;
    private ImageLoader imageLoader;
    private Context context;
	public PickGroupAdapter(Context context, int res, List<EMGroup> groups, ImageLoader imageLoader) {
		super(context, res, groups);
        this.context=context;
		this.inflater = LayoutInflater.from(context);
        this.imageLoader=imageLoader;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.row_group, null);
                }
                EMGroup emGroup = getItem(position);
                ((TextView) convertView.findViewById(R.id.name)).setText(emGroup.getGroupName());
                //添加消息数量
                TextView unReadMsg = (TextView) convertView.findViewById(R.id.unread_msg_number);
                TextView descTextView = (TextView) convertView.findViewById(R.id.desc);
                descTextView.setText(emGroup.getDescription());
                ImageView groupAvatar=(ImageView) convertView.findViewById(R.id.avatar);
                imageLoader.displayImage(URLs.MIHOME_GROUP_AVATAR + emGroup.getGroupId() + ".jpg?" + System.currentTimeMillis(),groupAvatar,options);
                convertView.setTag(emGroup);
		        return convertView;
	}

	@Override
	public int getCount() {
		return super.getCount();
	}

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.qunliao_common)
            .showImageForEmptyUri(R.drawable.qunliao_common) // resource or drawable
            .showImageOnFail(R.drawable.qunliao_common) // resource or drawable
            .resetViewBeforeLoading(false)  // default
            .cacheInMemory(false) // default
            .cacheOnDisk(true) // default
            .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
            .bitmapConfig(Bitmap.Config.ARGB_8888) // default
            .displayer(new SimpleBitmapDisplayer()) // default
            .handler(new Handler()) // default
            .build();
 }
